package com.qk.controller;

import com.qk.aop.anno.LogRecord;
import com.qk.common.Result;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: zhangchuang
 * @Date: 2025/10/13 08:12
 * @Description: 课程控制类
 */
@Slf4j
@RestController
@RequestMapping("/courses")
@SuppressWarnings("all")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping("/subject/{subject}")
    public Result getCourseBySubject(@PathVariable Integer subject) {
        log.info("获取课程列表，参数：{}", subject);
        return Result.success(courseService.getBySubject(subject));
    }

    @GetMapping("/list")
    public Result getCourseList() {
        log.info("获取课程列表");
        return Result.success(courseService.list());
    }

    @LogRecord
    @PutMapping
    public Result updateCourse(@RequestBody Course course) {
        log.info("修改课程，参数：{}", course);
        return courseService.updateCourseById(course) ? Result.success() : Result.error();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询课程，参数：{}", id);
        return Result.success(courseService.getById(id));
    }

    @LogRecord
    @PostMapping
    public Result addCourse(@RequestBody Course course) {
        log.info("新增课程，参数：{}", course);
        return courseService.addCourse(course) ? Result.success() : Result.error();
    }

    @LogRecord
    @DeleteMapping("/{id}")
    public Result deleteCourse(@PathVariable Integer id) {
        log.info("删除课程，参数：{}", id);
        return courseService.removeCourseById(id) ? Result.success() : Result.error();
    }

    @GetMapping
    public Result getCourseByPage(CourseDTO courseDTO) {
        log.info("获取课程列表，参数：{}", courseDTO);
        return Result.success(courseService.getCourseByPage(courseDTO));
    }

}
